package org.bigk.invoices.services;

import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.InvoicePurchaser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("invoicesPurchasersService")
@Transactional
public class InvoicesPurchasersServiceImpl implements InvoicesPurchasersService {

	private static final Logger logger =
		LoggerFactory.getLogger(InvoicesPurchasersServiceImpl.class);

	protected InvoicePurchaser invoicePurchaserTemplate;
	
	
	public InvoicesPurchasersServiceImpl() {
	}

	public InvoicePurchaser prepareNewInvoicePurchaser() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("prepareNewInvoicePurchaser() - start");
		}

		InvoicePurchaser ip = null;
		if (this.invoicePurchaserTemplate != null) {
			try {
				ip = invoicePurchaserTemplate.clone();
			} catch (CloneNotSupportedException e) {
				logger.warn("prepareNewInvoicePurchaser()", e);
			}
		}
		
		if (ip == null) {
			ip = new InvoicePurchaser();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("prepareNewInvoicePurchaser() - end - return value=" + ip);
		}
		return ip;
	}

	public InvoicePurchaser getInvoicePurchaserTemplate() {
		return invoicePurchaserTemplate;
	}

	public void setInvoicePurchaserTemplate(
			InvoicePurchaser invoicePurchaserTemplate) {
		this.invoicePurchaserTemplate = invoicePurchaserTemplate;
	}
}
